package nl.weeaboo.sh.level1;

import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import nl.weeaboo.game.gl.GLImage;
import nl.weeaboo.game.gl.GLManager;
import nl.weeaboo.game.input.UserInput;
import nl.weeaboo.sh.Game;
import nl.weeaboo.sh.danmaku.UniverseAdapter;
import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.level.Room;
import nl.weeaboo.sh.level.l99.Cirno;
import nl.weeaboo.sh.objects.Boss;
import nl.weeaboo.sh.objects.Platform;

public class Level1 extends Level {

	private GLImage bgI, moonI, sdmI;
	
	private int boss01Timeout = -1;
	private float moonRedness;
	
	public Level1(Game game) {
		super(game, 1, 0);
	}

	//Functions
	public void loadResources() {				
		super.loadResources();	
		
		if (game.config.getSoundVolume() >= 0.001f) {
			if (game.getFileExists("bgm.mp3")) {
				game.getSoundManager().setBGM("bgm.mp3", 5000);
			}
		}
		
		bgI = game.getImage("bg.png", true);		
		moonI = game.getImage("moon.png", true);
		sdmI = game.getImage("sdm.png", true);
	}
		
	public void update(UserInput input, float dt) {
		super.update(input, dt);
		
		boolean bossCreated = false;
		
		if (input.consumeKey(KeyEvent.VK_NUMPAD1)) {
			Skull enemy = new Skull(game, this);
			enemy.setXY(camera.getX() + 300, camera.getY() - 300);
			addSprite(enemy);
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD2)) {
			FlyingSkull enemy = new FlyingSkull(game, this);
			enemy.setXY(camera.getX() + 300, camera.getY());
			addSprite(enemy);
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD3)) {
			Platform enemy = new Platform(game, this, game.getImage("platform.png", true));
			enemy.setXY(camera.getX(), camera.getY());
			addSprite(enemy);
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD4)) {
			Chandelier enemy = new Chandelier(game, this, game.getImage("platform.png", true));
			enemy.setXY(camera.getX(), camera.getY());
			addSprite(enemy);
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD5)) {
			for (int n = 0; n < 100; n++) {
				Skull enemy = new Skull(game, this);
				enemy.setXY(camera.getX() + 300, camera.getY() - 300);
				addSprite(enemy);
			}
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD6)) {
			Boss01 boss = new Boss01(game, this);
			boss.setXY(camera.getX() + 300, camera.getY());
			setBoss(boss);
			
			bossCreated = true;
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD7)) {
			Cirno boss = new Cirno(game, this);
			boss.setXY(camera.getX() + 300, camera.getY());
			setBoss(boss);
			
			bossCreated = true;
		} else if (input.consumeKey(KeyEvent.VK_MULTIPLY)) {
			player.destroy();
		} else if (input.consumeKey(KeyEvent.VK_PAGE_UP)) {
			camera.setScale(camera.getScale() + 0.1f);
		} else if (input.consumeKey(KeyEvent.VK_PAGE_DOWN)) {
			camera.setScale(camera.getScale() - 0.1f);
		}
		
		if (bossCreated || input.consumeKey(KeyEvent.VK_DIVIDE)) {			
			if (getUniverseAdapter() == null) {
				Room room = getRoom(
						Level.toTileCoord(player.getX()),
						Level.toTileCoord(player.getY()));
				if (room != null) {
					setUniverseAdapter(new UniverseAdapter(game, room));
				}
			} else {
				setUniverseAdapter(null);
			}
		}
		
		if (boss01Timeout > 0) {
			boss01Timeout--;
			if (boss01Timeout == 0) {
				Room room = getRoom("boss01Room");
				setUniverseAdapter(new UniverseAdapter(game, room));

				int ts = Level.tileSize;
				
				Cirno boss = new Cirno(game, this);
				boss.setXY((room.getX()+room.getWidth() - 15) * ts,
						(room.getY()+room.getHeight()/2) * ts);
				setBoss(boss);				
			}
		}
	}
	
	public void onBossDefeated(Boss boss) {
		super.onBossDefeated(boss);
		
		setUniverseAdapter(null);
		Room room = getRoom("boss01Room");
		if (room != null) {
			room.setOpenSides(true, true, true, false);
			room.setWallSides(false, false, false, false);
		}
	}
	
	public void onPlayerChangeRoom(Room oldRoom, Room newRoom) {
		super.onPlayerChangeRoom(oldRoom, newRoom);
		
		if (newRoom == null) return;
		
		if (boss01Timeout == -1 && newRoom.getId().equals("boss01Room")) {
			boss01Timeout = 240;			
		}
	}
	
	public void drawBackground(GLManager glm, int w, int h) {
		int ts = Level.tileSize;
		Rectangle sb = area.getSpriteBounds();		
		sb = new Rectangle(sb.x*ts, sb.y*ts, sb.width*ts, sb.height*ts);
				
		moonRedness += (getUniverseAdapter() != null ? 0.01f : -0.05f);
		moonRedness = Math.max(0, Math.min(1, moonRedness));
		bgI.draw(glm, 0, 0, w, h);
		moonI.draw(glm, 180, 20, 128, 128);
		
		if (moonRedness >= 0.01f) {
			glm.pushColor();
			glm.setColor(1, 0, 0, moonRedness);
			bgI.draw(glm, 0, 0, w, h);
			moonI.draw(glm, 180, 20, 128, 128);
			glm.popColor();
		}		
		
		int sx = 50; int sy = 50;		
		int tx = Math.max(0, Math.min(sx, sx * (camera.getX()-sb.x) / sb.width));
		int ty = Math.max(0, Math.min(sy, sy * (camera.getY()-sb.y) / sb.height));
		//Y-axis doesn't work because the sprite bounds are infinite in the up dir.
		
		sdmI.draw(glm, -tx, h-400-ty, w+sx, 400+sy);
	}

	//Getters
	
	//Setters
	
}
